package net.zjitc.Listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {


    public void contextInitialized(ServletContextEvent sce) {


        ServletContext servletContext = sce.getServletContext();// 1.获取servletContext对象
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");// 2.获取配置文件路径
        ApplicationContext context = new ClassPathXmlApplicationContext(contextConfigLocation);// 3.获取spring容器对象
        servletContext.setAttribute("context",context);// 4.将spring容器对象保存到servletContext域中
        System.out.println("spring 容器初始化成功");// 5.输出信息
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
